package com.example.diagnose.Controller;

import com.example.diagnose.Config.CommonResponse;
import com.example.diagnose.Domain.Article;
import com.example.diagnose.Domain.FavoriteArticle;
import com.example.diagnose.Service.FavoriteArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/favorite_article")
@Tag(name = "用户收藏文章模块")
public class FavoriteArticleController {
    @Resource
    FavoriteArticleService favoriteArticleService;

    @Operation(summary = "获取用户收藏文章信息")
    @GetMapping("/{account}")
    public CommonResponse<List<Article>> getFavoriteArticle(@PathVariable("account") String account) {
        List<Article> articleList= favoriteArticleService.getFavoriteArticleByAccount(account);
        return CommonResponse.BackForSuccess("获得用户收藏文章信息成功", articleList);
    }

    @Operation(summary = "删除收藏文章")
    @DeleteMapping
    public CommonResponse<FavoriteArticle> deleteFavoriteArticleById(@RequestParam("id") int favorite_id){
        int result = favoriteArticleService.deleteFavoriteArticle(favorite_id);
        if(result == 0){
            return CommonResponse.BackForError("取消收藏失败");
        }else{
            return CommonResponse.BackForSuccess("取消收藏成功","http://127.0.0.1:8081/favorite_article");
        }
    }

    @Operation(summary = "添加收藏")
    @PostMapping
    public CommonResponse<FavoriteArticle> addFavoriteArticle(@RequestBody FavoriteArticle favoriteArticle){
        int result = favoriteArticleService.addFavoriteArticle(favoriteArticle);
        if(result == 0){
            return CommonResponse.BackForError("添加收藏失败");
        }else{
            return CommonResponse.BackForSuccess("添加收藏成功","http://127.0.0.1:8081/favorite_article");
        }
    }


}
